<?php
header("Content-type: text/html; charset=utf-8"); 
$host =  'http://www.uni.com/';
$url = 'http://fadmin.uni.com/api/search/myresult.html' ;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); //强制协议为1.0

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect: ")); //头部要送出'Expect: '
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); //强制使用IPV4协议解析域名
//curl_setopt($ch, CURLOPT_HEADER, 0);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_POST,1);


$data['keyword']=(isset( $_GET['keyword'])) ? $_GET['keyword'] : 'front';

//默认分类 id 为0
$data['category'] = (isset( $_GET['category'])) ? $_GET['category'] : '0';

//默认排序 为最新排序
$data['sort'] = (isset( $_GET['sort'])) ? $_GET['sort'] : 'new_down';

//默认页码 page 为1
$data['page'] = (isset( $_GET['page'])) ? $_GET['page'] : '1';

//每页查询条目数
$data['item'] = 40;

curl_setopt($ch,CURLOPT_POSTFIELDS,$data);

$res = curl_exec($ch);


$data = json_decode($res,TRUE);


if($data != '[]'){
	
	$categorylist  = $data['categorylist'];
	unset($data['categorylist']);
	
	$pagenum =  $data['pagenum'];
	unset($data['pagenum']);
	$product =  $data;
	
}else{
	$categorylist  = '[]';
	$product = '[]';
	
}


/* echo '<pre>';
var_dump($product);
echo '</pre>';
exit; */

//echo $errorinfo = json_last_error();   //输出4 语法错误


?>



<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>搜索测试</title>
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<script src="jquery-2.1.4.min.js"></script>
	</head>
	<style type="text/css">
		.tips {
    padding-right: 20px;
    color: red;
}

li{ 

text-align: left;
}



.pagelist{
	
	overflow: hidden;
    padding-bottom: 50px;
}


.pagelist li{
	float: left;
    width: 40px;
    text-decoration: none;
    list-style: none;
    background: blue;
    margin: 2px;
    text-align: center;
    color: white;
	cursor: pointer;
	
}


.pagelist li:active{
	background:#9e9fca
	
}
li.pageselect {
	   background: #9e9fca;
	   pointer-events: none;
}
.myhidden{
display:none;

}
.mykeyword{
	
	    color: red;
    font-weight: bold;
	
}
img{
	width: 30px;
	padding: 2px;
}
.p_content{
	
	
}
.p_attrimg{
	
	overflow: hidden;
    height: 30px;
	
}
.p_img{
	overflow: hidden;
    float: left;

	
	
}
ol li{
	
	
	padding-left:40px
}
</style>
	<body>
		<center>
		<form action="" method="get">
			<div class="mui-input-row mui-search">
			
				<input type="search" name="keyword" id="keyword" value = "<?php echo  $_GET['keyword']  ?>">
				
				
				<label> 分类：
				<select name="category" id="category" > 
				<option  value="0">all</option>
				<?php  foreach($categorylist as $item)  { ?>
				  <option  value =  "<?=$item['id_category']?>"  <?php   if($_GET['category'] ==$item['id_category'] ){echo  'selected';} ?>><?= $item['name']  ?></option>
				 
				 
				<?php  } ?>
				 
				</select>
				</label>
				<label> 排序：
				<select name="sort" id="sort" > 
				  <option  value='price_up'   <?php  if(@$_GET['sort'] == 'price_up'){echo  'selected';}?>   >价格从低到高</option>
				  <option  value='price_down' <?php  if(@$_GET['sort'] == 'price_down'){echo  'selected';}?>   >价格从高到低</option>
				  <option  value='sales_up'   <?php  if(@$_GET['sort'] == 'sales_up'){echo  'selected';}?>    >销量从低到高</option>
				  <option  value='sales_down'  <?php  if(@$_GET['sort'] == 'sales_down'){echo  'selected';}?>   >销量从高到低</option>
				  <option  value='new_up'     <?php  if(@$_GET['sort'] == 'new_up'){echo  'selected';}?>    >最老发布</option>
				  <option  value='new_down'    <?php  if(@$_GET['sort'] == 'new_down' or !isset($_GET['sort'])){echo  'selected';}?>   >最新发布</option>
				</select>
				</label>
				<input class="myhidden" type="text" name="page" value='1' >
				
				<input type="submit" value="搜索" />
			</div>
			
		</form>
		
		
		<h2>搜索结果</h2>
		<div >
			<ol>
			
			
			<?php  if(count($product) == 0) {
				
				echo  '<span  style="color:red;font-weight:bold">没有结果</span>';
			}  else{ 
			
			foreach($product  as $item)  { ?>
				<li  >

				<div class="p_img">
				<img src="<?php echo 'http://www.uni.com/'.$item['id_image'].'-small_default/'.$item['link_rewrite'].'.jpg'; ?>"  align="left" />
				</div>
				
				
				<div class="p_content">
				<a href = "<?php  echo  $host.$item['url']  ;?>.html">   
				<?php 
				$name = str_ireplace($_GET['keyword'],"<span class='mykeyword'> ".$_GET['keyword']."</span>",$item['name']); 
				
				if($item['oldprice'] == $item['realprice'] ){
					
					echo  " [". $item['id_product']."]"."<SPAN style='color:red'>[".  number_format($item['oldprice'],2)."]</span>" .$name   ;
				}else{
					$price =  "<SPAN style='TEXT-DECORATION: line-through'>[".  number_format($item['oldprice'],2)."]</span>" . "<SPAN style='color:red'>[".  number_format($item['realprice'],2)."]</span>" ;
					
					echo  "[". $item['id_product']."]" . $price .$name   ;
				}
			
				?></a>
				</div>
				<div class="p_attrimg">
				<?php  if( $item['attrimg'] !=''){ foreach($item['attrimg'] as $imgitem)   { ?>
				
				<img src="<?php echo $imgitem['imgurl'] ?>"  align="left" />
				<?php  } }?>
				</div>
				</li>
			<?php   }} ?>

				
			</ol>
		</div>
		
		<h3>分页</h3>
		 <ul class="pagelist">
		 
			<?php  if($pagenum =='0'){ ?>
			
			<?php }else{ for($i=1;$i<=$pagenum;$i++)  {  ?>

			 <li   onclick ="gopage(this)"  <?php if( $i== $_GET['page'] )  { echo  'class="pageselect"';} ?>   ><?php echo $i ?></li>

			<?php } } ?>
		 </ul>
		
		</center>
		<script type="text/javascript">
		  function  gopage(obj){
			 var keyword = $('#keyword').val();
			 var category = $('#category').val();
			 var sort = $('#sort').val();
			 var page =  obj.innerHTML;
			 var url = "/searchresult.php?keyword="+keyword+ "&category="+category+"&sort="+sort+"&page="+page;
			 //alert(url);
			 location.href=url;
		  }
		</script>
	</body>
</html>